#!BPY
"""
Name: 'TLS Exporter'
Blender: 249
Group: 'Export'
Tip: 'Export TLS File'

"""
from __future__ import print_function
from Blender import *


scn=Scene.GetCurrent()
objs=scn.objects

cam=[]
lights=[]
mats=[]

def notin(x):
    for i in mats:
        if x==i:
            return False
    return True

def get_index(x):
    cnt=0
    for i in mats:
        if x==i:
            return cnt
        cnt=cnt+1

for i in objs:
    if i.getType()=='Camera':
        cam=i
    elif i.getType()=='Lamp':
        lights.append(i)
    else:
        m=i.getData(mesh=1).materials[0];
        if notin(m):
            mats.append(m)


result=open("scene.txt","w")
print("16",file=result)
print(len(lights),file=result)
print(len(mats),file=result)

tmp=0
for i in objs:
    if i.getType()=='Mesh':
        tmp=tmp+1

print(tmp,file=result)


print("\n",file=result)
m=cam.getMatrix()
for i in m:
    print("%f %f %f %f"%(i[0],i[1],i[2],i[3]),file=result)

d=cam.getData()
print("%f"%d.angle,file=result)
print("4 3",file=result)
context=scn.getRenderingContext()
w=context.sizeX
h=context.sizeY
print("%d %d"%(w,h),file=result)


print("\n",file=result)
for i in lights:
    d=i.getData()
    print("\n",file=result)
    print("%f %f %f"%(i.loc[0],i.loc[1],i.loc[2]),file=result)
    print("%f %f %f"%(d.energy*d.col[0],d.energy*d.col[1],d.energy*d.col[2]),file=result)


print("\n",file=result)
for i in mats:
    print("%f %f %f"%(i.ref*i.rgbCol[0],i.ref*i.rgbCol[1],i.ref*i.rgbCol[2]),file=result)

    
print("\n",file=result)
for i in objs:
    if i.getType()=='Mesh':
        m=i.getMatrix()
        for j in m:
            print("%f %f %f %f"%(j[0],j[1],j[2],j[3]),file=result)
        index=get_index(i.getData(mesh=1).materials[0])
        print(index,file=result)
        print(i.getName(),file=result)


        
